package com.lzk.system.service.impl;

import com.lzk.basic.service.impl.BaseServiceImpl;
import com.lzk.system.domain.Dictionarydetail;
import com.lzk.system.domain.Dictionarytype;
import com.lzk.system.mapper.DictionarydetailMapper;
import com.lzk.system.mapper.DictionarytypeMapper;
import com.lzk.system.service.IDictionarytypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Service
public class DictionarytypeServiceImpl extends BaseServiceImpl<Dictionarytype> implements IDictionarytypeService {

    @Autowired
    private DictionarytypeMapper dictionarytypeMapper;

    @Autowired
    private DictionarydetailMapper dictionarydetailMapper;

    @Override
    @Transactional
    public void remove(Long id) {
            delDetail(id);
            dictionarytypeMapper.remove(id);

    }

    @Override
    public void patchRemove(List<Long> ids) {
        for (Long id : ids) {
            delDetail(id);
        }

        dictionarytypeMapper.patchRemove(ids);
    }

    public void delDetail(Long id) {
        try {
            //通过类型ID查询所有对应的明细
            List<Dictionarydetail> dictionarydetails = dictionarydetailMapper.loadByTypeAll(id);
            ArrayList<Long> ids = new ArrayList<>();
            if (dictionarydetails != null) {
                for (Dictionarydetail dictionarydetail : dictionarydetails) {
                    Long did = dictionarydetail.getId();
                    ids.add(did);
                }
                dictionarydetailMapper.patchRemove(ids);
            }
        } catch (Exception e) {
        }
    }

}
